package com.mlm.mlmapp.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.adapter.view.AQProgressDialog;
import com.mlm.mlmapp.adapter.view.CustomDialog;
import com.mlm.mlmapp.adapter.view.adapter.MainAdapter;
import com.mlm.mlmapp.adapter.view.listener.OnBannerClickedListener;
import com.mlm.mlmapp.adapter.view.listener.OnGridItemSelectedListener;
import com.mlm.mlmapp.adapter.view.listener.OnSyClickedListener;
import com.mlm.mlmapp.cons.HTTPCons;

@EActivity(R.layout.activity_main)
public class MainActivity extends BaseActivity implements
		OnGridItemSelectedListener {
	private AQuery aq;
	private MainAdapter adapter;
	private RecyclerView recyclerView;
	private JSONArray array;

	@AfterViews
	public void init() {

		// btn_home.setBackgroundColor(getResources().getColor(R.color.red));
		btn_home.setCompoundDrawablesWithIntrinsicBounds(null, getResources()
				.getDrawable(R.drawable.homechecked), null, null);
		btn_home.setTextColor(getResources().getColor(R.color.red));
		setTranStatusBar();
		aq = new AQuery(this);
		recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
		final GridLayoutManager layoutManager = new GridLayoutManager(this, 1);
		layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
			@Override
			public int getSpanSize(int position) {
				return adapter.isPositionHeader(position) ? layoutManager
						.getSpanCount() : 1;
			}
		});
		recyclerView.setLayoutManager(layoutManager);
		// recyclerView.addItemDecoration(new GridMarginDecoration(this, 1, 1,
		// 1, 1));
		recyclerView.setHasFixedSize(true);
		aq.progress(new AQProgressDialog(this)).ajax(HTTPCons.get_styles_action, JSONArray.class,
				new AjaxCallback<JSONArray>() {

					@Override
					public void callback(String url, JSONArray object,
										 AjaxStatus status) {
						// TODO Auto-generated method stub
						super.callback(url, object, status);
						if (status.getCode() == -101) {
							Toast.makeText(aq.getContext(), "网络异常,请检查网络",
									Toast.LENGTH_SHORT);
							return;
						}
						array = object;
						if (object == null) {
							object = new JSONArray();
						}
						adapter = new MainAdapter(MainActivity.this, object,
								MainActivity.this,
								new OnBannerClickedListener() {

									@Override
									public void onItemClick(View v, String url) {
										// TODO Auto-generated method stub
										if (url != null && !"".equals(url))

											ShowWebView_
													.intent(MainActivity.this)
													.extra("url", url).start();
									}
								}, new OnSyClickedListener() {

							@Override
							public void onSyItemClick(View v, String id) {
								// TODO Auto-generated method stub
								HairDresserDetailActivity_
										.intent(MainActivity.this)
										.extra("id", id).start();
							}
						});
						recyclerView.setAdapter(adapter);
					}

				});

	}

	@Override
	public void onGridItemClick(View v, int position) {
		// Toast.makeText(MainActivity.this, "Grid item clicked position is " +
		// position, Toast.LENGTH_SHORT).show();

	}

	@Override
	public void onItemAppointment(View v, int position) {
		// TODO Auto-generated method stub

		if (position > 0) {
			try {
				JSONObject obj = array.getJSONObject(position - 1);
				Log.d("sort",obj+"");
					BookActivity_.intent(this)
						.extra("id", obj.getString("stylistId")).extra("styleid", obj.getString("id")).flags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).start();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onItemCollection(View v, int position) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onHairStyleClick(View v, int position) {
		// TODO Auto-generated method stub
		if (position > 0) {
			try {
				JSONObject obj = array.getJSONObject(position - 1);
				HairStyleDetailActivity_.intent(this)
						.extra("id", obj.getString("id")).start();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// HairStyleActivity_.intent(this).extra("id",position);
	}

	@ViewById
	Button btn_home;

	@Click
	void btn_home() {
		// MainActivity_.intent(this).start();

	}

	@Click
	void btn_dresser() {
		HairDresserListActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).start();
	}

	@Click
	void btn_appointment() {
		BookActivity_.intent(this).start();
	}

	@Click
	void btn_style() {
		HairStyleListActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).start();
	}

	@Click
	void btn_userinfo() {
		UserInfoActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).start();
	}
}
